package co.yixiang.modules.system.service.impl;

import co.yixiang.modules.system.domain.VersionDesc;
import cn.hutool.core.convert.Convert;
import co.yixiang.common.service.impl.BaseServiceImpl;
import com.alibaba.fastjson.JSONArray;
import lombok.AllArgsConstructor;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import co.yixiang.common.utils.QueryHelpPlus;
import co.yixiang.utils.ValidationUtil;
import co.yixiang.utils.FileUtil;
import co.yixiang.modules.system.service.VersionDescService;
import co.yixiang.modules.system.dto.VersionDescDto;
import co.yixiang.modules.system.dto.queryCriteria.VersionDescQueryCriteria;
import co.yixiang.modules.system.mapper.VersionDescMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.util.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @author DB
* @date 2020-09-08
*/
@Service
@AllArgsConstructor
//@CacheConfig(cacheNames = "versionDesc")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class VersionDescServiceImpl extends BaseServiceImpl<VersionDescMapper, VersionDesc> implements VersionDescService {

    @Override
    //@Cacheable
    public Map<String, Object> queryAll(VersionDescQueryCriteria criteria, Pageable pageable) {
        getPage(pageable);
        PageInfo<VersionDesc> page = new PageInfo<>(queryAll(criteria));
        Map<String, Object> map = new LinkedHashMap<>(2);
        map.put("content", Convert.toList(VersionDescDto.class, page.getList()));
        map.put("totalElements", page.getTotal());
        return map;
    }


    @Override
    //@Cacheable
    public List<VersionDesc> queryAll(VersionDescQueryCriteria criteria){
        return baseMapper.selectList(QueryHelpPlus.getPredicate(VersionDesc.class, criteria));
    }

    @Override
    //@Cacheable
    public VersionDesc queryOne(VersionDescQueryCriteria criteria){
        return this.getOne(QueryHelpPlus.getPredicate(VersionDesc.class, criteria));
    }


    @Override
    public void download(List<VersionDescDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (VersionDescDto versionDesc : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("版本号", versionDesc.getVersionName());
//            map.put("下载地址", versionDesc.getDownloadUrl());
            map.put("版本更新信息",versionDesc.getNote());
            map.put("1大更新  0小更新",versionDesc.getUpStatus());
            map.put("是否有更新",versionDesc.getUpdateStatus());
            map.put("AppStore，用于整包升级的方式",versionDesc.getPkgUrl());
            map.put("wgt包下载路径，用wgt方式更新",versionDesc.getWgtUrl());
            map.put("创建时间", versionDesc.getCreateTime());
            map.put("修改时间", versionDesc.getUpdateTime());
            list.add(map); }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public String queryVersion(){
        VersionDesc versionDesc = queryOne(VersionDescQueryCriteria.builder().id(1).build());
        HashMap<String, String> map = new HashMap<>();
        map.put("PkgUrl",versionDesc.getPkgUrl());
        map.put("WgtUrl",versionDesc.getWgtUrl());
        map.put("backdrop",versionDesc.getBackdrop());
        Object objMap= JSONArray.toJSON(map);
        String jsonMap = objMap.toString();
        return jsonMap;
    }
}
